<template>
  <el-container class="main_con">
    <ufrom @submit="saveData" ref="ufrom">
      <el-form size="medium" label-width="160px" ref="itemForm" :model="obj.configuration" :rules="itemRules">
        <div class="setting_body_item">
          <div class="conetnt edit_con">
            <div class="edit_content">
              <el-form-item label="未付款订单" prop="">
                <el-radio-group v-model="is_close">
                  <el-radio :label="0">永不关闭</el-radio>
                  <el-radio :label="1">自定义关闭时间</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item v-if="is_close == 1" label="" prop="close_time">
                <div class="fks">
                  订单拍下
                  <el-input v-model.number="obj.configuration.close_time" style="width:180px;margin-right:10px;margin-left:10px">
                    <template slot="append">
                      分钟
                    </template>
                  </el-input>
                  内未付款，自动关闭订单
                  <div style="color:#909399;font-size:12px;">订单取消时间必须在20-1440分钟之间</div>
                </div>
              </el-form-item>
            </div>
          </div>
        </div>
      </el-form>
    </ufrom>
  </el-container>
</template>

<script>
import {addSiteConfig, editSiteConfig, getSiteConfig} from '@/api/mall/setting';
export default {
  data() {
    return {
      isLoading: false,
      is_close: 0,
      rk: 1,
      obj: {
        type: 'seckill',
        status: 1,
        configuration: {
          close_time: 0
        }
      },

      itemRules: {
        close_time: [
          {
            required: true,
            message: '订单取消时间必须在20-1440分钟之间'
          },
          {type: 'number', min: 20, max: 1440, message: '订单取消时间必须在20-1440分钟之间', trigger: 'blur'}
        ],
        logo: [
          {
            required: true,
            message: '商城logo必须上传'
          }
        ]
      }
    };
  },

  mounted() {
    this.getConfig('seckill');
  },
  methods: {
    //获取站点配置数据
    getConfig(type) {
      this.isLoading = true;
      getSiteConfig(type)
        .then(res => {
          this.isLoading = false;
          if (res.msg) {
            this.obj = res.msg;
            if (this.obj.configuration.close_time) {
              this.is_close = 1;
            } else {
              this.is_close = 0;
            }
          }
        })
        .catch(e => {
          this.isLoading = false;
        });
    },

    //保存数据
    saveData() {
      this.$refs.itemForm.validate(valid => {
        if (valid) {
          this.isLoading = true;
          if (this.is_close == 0) {
            this.obj.configuration.close_time = 0;
          } else {
            if (this.obj.configuration.close_time < 5) {
              this.obj.configuration.close_time = 5;
            } else if (this.obj.configuration.close_time > 1440) {
              this.obj.configuration.close_time = 1440;
            }
          }

          if (this.obj.update_time) {
            //更新
            editSiteConfig(this.obj)
              .then(res => {
                this.$refs.ufrom.init();
                this.getConfig('seckill');
              })
              .catch(e => {
                this.$refs.ufrom.init();
              });
          } else {
            //新增
            addSiteConfig(this.obj)
              .then(res => {
                this.$refs.ufrom.init();
                this.getConfig('seckill');
              })
              .catch(e => {
                this.$refs.ufrom.init();
              });
          }
        } else {
          this.$refs.ufrom.init();
          //校验没通过
          return false;
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.configuration {
  .el-input {
    width: 120px;
    margin: 0px 5px;
  }
}
</style>
